package com.hospital.ui.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.LinearLayout;
import android.widget.SlidingDrawer;

import com.hospital.R;
import com.hospital.ui.adapter.MainMenuExpandableListAdapter;
import com.hospital.ui.widget.BasePageLayout;
import com.hospital.ui.widget.MainMenuExpandableList;
import com.hospital.utils.LogUtils;
import com.hospital.utils.MainUtils;
import com.hospital.vo.Page;

public class MainActivity extends Activity {
	private final static String TAG = "MainActivity";
	private final static String RADIO_BTN_SELECTED_COLOR = "#ff1482ff";
	private final static String RADIO_BTN_UNSELECTED_COLOR = "#fff9f9f9";

	private final static int PAGE_CACHE_MAX_SIZE = 10;
	private LinearLayout mViewer;
	private MainMenuExpandableList mMainMenu;
	private Button mRadioBtnP;
	private Button mRadioBtnZ;
	private Button mRadioBtnT;
	private Button mRadioBtnH;
	private SlidingDrawer mDrawer;

	private Page mCurrentPage;

	private PageCache mPageCache;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
		mPageCache = new PageCache(PAGE_CACHE_MAX_SIZE);
		mCurrentPage = new Page();
		mViewer = (LinearLayout) findViewById(R.id.viewer);
		mMainMenu = (MainMenuExpandableList) findViewById(R.id.main_menu);
		mMainMenu.setAdapter(new MainMenuExpandableListAdapter(this));

		mMainMenu.setOnGroupCollapseListener(new OnGroupCollapseListener() {

			@Override
			public void onGroupCollapse(int groupPosition) {
				// TODO Auto-generated method stub
				LogUtils.i(TAG, "onGroupCollapse");
				mMainMenu.setGroupTextNormalColor(groupPosition);
			}
		});

		mMainMenu.setOnGroupExpandListener(new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int groupPosition) {
				// TODO Auto-generated method stub
				LogUtils.i(TAG, "onGroupExpand groupPosition = "
						+ groupPosition);
				if (mMainMenu.mCurrentGroupPos != groupPosition) {
					mMainMenu.collapseCurrentGroup();
					mMainMenu.mCurrentGroupPos = groupPosition;
				}
				mMainMenu.setGroupTextSelectedColor(groupPosition);
				mMainMenu.setSelectedChild(groupPosition, -1, false);
			}
		});

		mMainMenu.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				LogUtils.i(TAG, "onChildClick groupPosition = " + groupPosition
						+ " childPosition = " + childPosition);
				switchPage(MainUtils.genKey(groupPosition, childPosition));
				return false;
			}
		});

		mRadioBtnP = (Button) findViewById(R.id.button_radio_a);
		mRadioBtnZ = (Button) findViewById(R.id.button_radio_b);
		mRadioBtnT = (Button) findViewById(R.id.button_radio_c);
		mRadioBtnH = (Button) findViewById(R.id.button_radio_d);
		mRadioBtnP.setOnClickListener(mBtnOnClickListener);
		mRadioBtnZ.setOnClickListener(mBtnOnClickListener);
		mRadioBtnT.setOnClickListener(mBtnOnClickListener);
		mRadioBtnH.setOnClickListener(mBtnOnClickListener);
		loadInitPage();
		setRadioBtnPSelected();
	}

	private OnClickListener mBtnOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switchRadioBtn(v.getId());
		}
	};

	private void switchRadioBtn(int id) {
		switch (id) {
		case R.id.button_radio_a:
			setRadioBtnPSelected();
			setMainMenuP();
			break;
		case R.id.button_radio_b:
			setRadioBtnZSelected();
			setMainMenuZ();
			break;
		case R.id.button_radio_c:
			setRadioBtnTSelected();
			setMainMenuT();
			break;
		case R.id.button_radio_d:
			setRadioBtnHSelected();
			setMainMenuH();
			break;
		default:
			break;
		}
	}

	private void setRadioBtnPSelected() {
		mRadioBtnP.setTextColor(Color.parseColor(RADIO_BTN_SELECTED_COLOR));
		mRadioBtnZ.setTextColor(Color.parseColor(RADIO_BTN_UNSELECTED_COLOR));
		mRadioBtnT.setTextColor(Color.parseColor(RADIO_BTN_UNSELECTED_COLOR));
		mRadioBtnH.setTextColor(Color.parseColor(RADIO_BTN_UNSELECTED_COLOR));
	}

	private void setRadioBtnZSelected() {
		mRadioBtnP.setTextColor(Color.parseColor(RADIO_BTN_UNSELECTED_COLOR));
		mRadioBtnZ.setTextColor(Color.parseColor(RADIO_BTN_SELECTED_COLOR));
		mRadioBtnT.setTextColor(Color.parseColor(RADIO_BTN_UNSELECTED_COLOR));
		mRadioBtnH.setTextColor(Color.parseColor(RADIO_BTN_UNSELECTED_COLOR));
	}

	private void setRadioBtnTSelected() {
		mRadioBtnP.setTextColor(Color.parseColor(RADIO_BTN_UNSELECTED_COLOR));
		mRadioBtnZ.setTextColor(Color.parseColor(RADIO_BTN_UNSELECTED_COLOR));
		mRadioBtnT.setTextColor(Color.parseColor(RADIO_BTN_SELECTED_COLOR));
		mRadioBtnH.setTextColor(Color.parseColor(RADIO_BTN_UNSELECTED_COLOR));
	}

	private void setRadioBtnHSelected() {
		mRadioBtnP.setTextColor(Color.parseColor(RADIO_BTN_UNSELECTED_COLOR));
		mRadioBtnZ.setTextColor(Color.parseColor(RADIO_BTN_UNSELECTED_COLOR));
		mRadioBtnT.setTextColor(Color.parseColor(RADIO_BTN_UNSELECTED_COLOR));
		mRadioBtnH.setTextColor(Color.parseColor(RADIO_BTN_SELECTED_COLOR));
	}

	private void setMainMenuP() {
		mMainMenu.expandGroup(0);
		mMainMenu.setSelectedChild(0, 1, false);
		switchPage(MainUtils.genKey(0, 1));
	}

	private void setMainMenuZ() {
		mMainMenu.expandGroup(1);
		mMainMenu.setSelectedChild(1, 1, false);
		switchPage(MainUtils.genKey(1, 1));
	}

	private void setMainMenuT() {
		mMainMenu.expandGroup(4);
		mMainMenu.setSelectedChild(4, 5, false);
		switchPage(MainUtils.genKey(4, 5));
	}

	private void setMainMenuH() {
		mMainMenu.expandGroup(2);
		mMainMenu.setSelectedChild(2, 3, false);
		switchPage(MainUtils.genKey(2, 3));
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		} else {
			return super.onKeyUp(keyCode, event);
		}
	}

	private BasePageLayout getPageFromCache(String key) {
		if(mPageCache != null){
			return mPageCache.getPage(key);
		}
		return null;
	}

	private void removeCurrentPage() {
		mViewer.removeAllViews();
	}

	private void addPage(BasePageLayout page) {
		if (mViewer != null) {
			mViewer.addView(page);
		}
	}

	private void savePage2Cache(String key, BasePageLayout page) {
		if(mPageCache != null){
			mPageCache.addPage(key, page);
		}
	}

	private BasePageLayout loadPage(int rId) {
		LogUtils.i(TAG, "loadPage rid = " + rId);
		LayoutInflater inflater = LayoutInflater.from(this);
		BasePageLayout page = (BasePageLayout) inflater.inflate(rId, null);
		return page;
	}

	private void loadInitPage() {
		switchPage(MainUtils.genKey(0, 1));
	}

	private void switchPage(String key) {
		if ((mCurrentPage.key == null)
				|| ((mCurrentPage.key != null)
						&& (key != null) && !(mCurrentPage.key
							.equalsIgnoreCase(key)))) {
			LogUtils.i(TAG, "switchPage key = " + key);
			BasePageLayout page = getPageFromCache(key);
			if (page == null) {
				int rid = MainUtils.getPageRid(key);
				if (rid != MainUtils.PAGE_RID_NONE) {
					page = loadPage(rid);
				}
			}
			if (page != null) {
				removeCurrentPage();
				addPage(page);
				savePage2Cache(key, page);
			} else {
				removeCurrentPage();
			}
			mCurrentPage.key = key;
		}
	}

}